#ifndef _M5IMGDEC_H_
#define _M5IMGDEC_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ezfb_types.h"

// image file type supported 

#define IMG_MOD_GRAY        0
#define IMG_MOD_16BPP		1 
#define IMG_MOD_24BPP	    2

#define TP_PNG  ".png"
#define TP_GIF  ".gif" 
#define TP_JPEG ".jpg"
#define TP_TGA  ".tga"
#define TP_PCX  ".pcx"
#define TP_WBMP ".wbmp"
#define TP_WMF  ".wmf"
#define TP_ICO  ".ico"
#define TP_BMP  ".bmp"

typedef struct _M5FRAME_INFO_ {
	int frm_left ; 
	int frm_top ; 
	int frm_width ; 
	int frm_height ; 
	int whole_width ; 
	int whole_height ; 
	int delay ; 
	int flag ; 
} M5FRAME_INFO ; 

class CxImage ; 

class CM5ImgDec {
private:
	CxImage * ximg ;

private: 
	int  GetImgType(const char * img_pathname) ; 
	void StrLower(char *obj, const char * src) ; 
	unsigned short fast_div(unsigned short a) {
		const unsigned short s = 0x0ffffu / 0x0ff ; 
		unsigned short q, r ; 
		q = (unsigned short)(((unsigned int)(a) * s) >> 16) ; 
		r = a * 0x0ff ; 
		if(r >=0x0ff) q++ ;
		return q ;  
	}
public:
	CM5ImgDec() ; 
	virtual ~CM5ImgDec() ; 

	bool Decode(const char * img_name) ; 
	bool Decode(u_char * buffer, u_short size, u_short imagetype) ;
	
	bool CreateMemBmp(int w, int h, int bpp) ; 
	bool CopyMemBmp(CM5ImgDec& src) ; 
	unsigned char * GetBits() ; 
	const CxImage * GetCxImage() ; 
	
	// decoder info 
	int  GetWidth() ; 
	int  GetHeight() ;
	int  GetFrameCount() ; 
	bool HasAlpha(int frm_idx = 0) ;  
	
	// operation functions 
	bool Resize(int new_width, int new_height) ; 
	bool RotateLeft() ; 
	bool RotateRright() ; 
	bool Rotate180() ; 
	bool Mirror() ; 
	
	// copy result to CFbsBitmap for latter operation 
	bool GetFrame(u_char * res_bmp, 
				  int frm_idx = 0,
				  int bmp_mode = IMG_MOD_16BPP) ; 

	bool GetFrame1(u_char * res_bmp, 
				  int frm_idx = 0,
				  int bmp_mode = IMG_MOD_16BPP) ; 

	bool GetFrameAlpha(u_char* res_bmp, 
					   int frm_idx = 0,
					   int bmp_mode = IMG_MOD_GRAY) ; 
	
	bool GetFrameInfo(M5FRAME_INFO * frm_info, int frm_idx = 0) ; 

	// prepare for next decode 
	bool ResetImgDec() ; 
} ; 

#endif
